package cn.ekuma.data.ui.android;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import cn.ekuma.data.dao.bean.I_ViewBean;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public abstract class AbstractViewBeanDetailActivate<T extends I_ViewBean> extends Activity {
	
	public static final String SELECTED_BEAN="selected_bean";
	public static final int MSG_SUCCESS = 0;
    public static final int MSG_FAILURE = 1;
    
	ExpandableListView expList;
	protected MyListExpandableListAdapter listAdatper;

	protected ArrayList<String> groupName=new ArrayList<String>();
	protected HashMap<String,List<I_ViewBean>> groupLinkData=new HashMap<String,List<I_ViewBean>>();
	protected T obj;
	
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.abstract_detail_view);
		expList=(ExpandableListView) findViewById(R.id.detail_view_expList);
		Bundle mBundle=getIntent().getExtras();
		obj=(T) mBundle.getSerializable(SELECTED_BEAN);
		listAdatper=new MyListExpandableListAdapter();
		expList.setAdapter(listAdatper);
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		groupName=new ArrayList<String>();
		initData();
		expList.expandGroup(0);
	}
	
	protected abstract void initData();

	public class MyListExpandableListAdapter extends BaseExpandableListAdapter{

		
		public int getGroupCount() {
			
			return 1+groupName.size();
		}

		
		public int getChildrenCount(int groupPosition) {
			if(groupPosition==0)
				return 1;
			return groupLinkData.get(groupName.get(groupPosition-1)).size();
		}

		
		public Object getGroup(int groupPosition) {
			return groupPosition;
		}

		
		public Object getChild(int groupPosition, int childPosition) {
			return groupPosition+childPosition;
		}

		
		public long getGroupId(int groupPosition) {
			return groupPosition;
		}

		
		public long getChildId(int groupPosition, int childPosition) {	
			return groupPosition+childPosition;
		}

		
		public boolean hasStableIds() {
			// TODO Auto-generated method stub
			return false;
		}

		
		public View getGroupView(int groupPosition, boolean isExpanded,
				View convertView, ViewGroup parent) {
			View view = LayoutInflater.from(AbstractViewBeanDetailActivate.this).inflate( R.layout.expandable_group, null);
			ImageView mgroupimage=(ImageView)view.findViewById(R.id.groupimage);      
			if(!isExpanded){         
				mgroupimage.setBackgroundResource(R.drawable.cand_down_press);         
				}else{             
					mgroupimage.setBackgroundResource(R.drawable.cand_up_press);  
				}            
			TextView text = (TextView) view.findViewById(R.id.groupname);           
			
		     if(groupPosition==0)
		    	 text.setText("基本明细"); 
		     else
		    	 text.setText(groupName.get(groupPosition-1)); 
		     return view;
		}

		
		public View getChildView(int groupPosition, int childPosition,
				boolean isLastChild, View convertView, ViewGroup parent) {
			if(groupPosition==0)
				return getVeiwBeanDetailView(obj);
			else
				return getChildView(groupPosition-1,childPosition,isLastChild, convertView, parent);
		}

		
		public boolean isChildSelectable(int groupPosition, int childPosition) {
			return false;
		}	
	}

	public abstract View getVeiwBeanDetailView(T obj);
	
	public abstract View getChildView(int groupPosition, int childPosition,boolean isLastChild, View convertView, ViewGroup parent);
	
	protected Handler mHandler = new Handler() {  
        public void handleMessage (Message msg) {//此方法在ui线程运行   
            switch(msg.what) {  
            case MSG_SUCCESS:  
            	groupName=new ArrayList<String>();
            	groupLinkData=(HashMap<String, List<I_ViewBean>>) msg.obj;
            	for(String key:groupLinkData.keySet())
            		groupName.add(key);
            	listAdatper.notifyDataSetChanged();
                break;  
            case MSG_FAILURE:  
                Toast.makeText(getApplication(), "执行出错了", Toast.LENGTH_LONG).show();  
                break;
        }  
    }
 };
	
	
	protected View createTextView(String text1){
		View view = LayoutInflater.from(AbstractViewBeanDetailActivate.this).inflate( R.layout.list_item, null);
	     TextView text = (TextView) view.findViewById(R.id.text);
	     text.setText(text1); 
	     return view;
	}
	
	public int getRealPosition(String[] values,String num){
		int i=0;
		for(String s:values){
			if(num.equalsIgnoreCase(s))
				break;
			i++;
		}
		return i;
	}
}
